package com.shiguiwu.springboot3.filter;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SensitiveFilter {
    private Node root = new Node();

    public void addWord(String word) { /* 构建 Trie */ }

    public boolean containsSensitive(String text) {
        // Aho-Corasick 更强，但这里给出简单 sliding window Trie 检测
        for (int i=0;i<text.length();i++){
            Node p = root;
            for (int j=i;j<text.length();j++){
                char c = text.charAt(j);
                p = p.children.get(c);
                if (p == null) break;
                if (p.isEnd) return true;
            }
        }
        return false;
    }
    static class Node { Map<Character,Node> children = new HashMap<>(); boolean isEnd; }
}